package com.server.controller;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mysql.fabric.xmlrpc.base.Data;
import com.server.model.OsaUserInfo;
import com.server.model.OsaUserOrderInfos;
import com.server.service.IOsaCashBackOrderService;
import com.server.service.IOsaUserOrderInfosService;
import com.server.utils.BaseResult;
import com.server.utils.UserSessionMapUtils;

@RestController
@RequestMapping(value = "/userOrderInfos")
public class OsaUserOrderInfosController {

	private Logger logger = Logger.getLogger(OsaUserOrderInfosController.class);
	
	@Autowired
	private IOsaCashBackOrderService iOsaCashBackOrderService;
	
	@Autowired
	private IOsaUserOrderInfosService iOsaUserOrderInfosService;
	
	/**
	 * 用户录单
	 * @param params 订单参数
	 * @return
	 */
	@RequestMapping(value="/addUserOrderInfo")
	public JSONObject addOsaUserOrderInfos(@RequestParam Map<String, Object> params) {
		logger.info("用户录单");
		logger.info(JSON.toJSON(params));
		BaseResult result = new BaseResult();
		String sessionCode = params.get("sessionCode").toString();
		OsaUserInfo user = UserSessionMapUtils.getUserInfo(sessionCode);
		if (user == null) { // 会话失效
			result.setCode("102");
			result.setMsg("session失效，请重新登录");
		} else {
			
			String orderNum = params.get("orderNum").toString();
			OsaUserOrderInfos orderInfos = iOsaUserOrderInfosService.selectUserOrderInfoByOrderNum(orderNum);
			if(orderInfos == null) {
				String phoneNum = params.get("phoneNum").toString();
				String expressUserName = params.get("expressUserName").toString();
				int isProductPay = Integer.valueOf(params.get("isProductPay").toString()); // 签收方式
				String productName = params.get("productName").toString();
				int productNum = Integer.valueOf(params.get("productNum").toString());
				String settlement = params.get("settlement").toString();
				Double selfPay = Double.valueOf(params.get("selfPay").toString());
				int  employedType = Integer.valueOf(params.get("employedType").toString());
				String remark = params.get("remark").toString();
				logger.info("订单号可以录入");
				OsaUserOrderInfos osaUserOrderInfos = new OsaUserOrderInfos();
				osaUserOrderInfos.setOrderNum(orderNum);
				osaUserOrderInfos.setPhoneNum(phoneNum);
				osaUserOrderInfos.setExpressUserName(expressUserName);
				long currentTime = System.currentTimeMillis();
				String t = String.valueOf(currentTime); 
				String seconds = t.substring(0, 10);
				osaUserOrderInfos.setInsertTime(Integer.valueOf(seconds));
				osaUserOrderInfos.setIsProductPay(isProductPay);
				osaUserOrderInfos.setProductName(productName);
				osaUserOrderInfos.setProductNum(productNum);
				osaUserOrderInfos.setSettlement(settlement);
				osaUserOrderInfos.setSelfPay(selfPay);
				osaUserOrderInfos.setUserCode(user.getUserCode());
				osaUserOrderInfos.setEmployedType(employedType);
				osaUserOrderInfos.setRemark(remark);
				
				int id = iOsaUserOrderInfosService.insertUserOrderInfo(osaUserOrderInfos);
				UserSessionMapUtils.putUser(sessionCode, user);
				result.setCode("200");
				result.setMsg("提交成功");
			} else {
				result.setCode("201");
				result.setMsg("该订单号（物流单号）已经存在系统中，不可重复录入，如有疑问请联系客服人员");
			}
		}
		
		return JSONObject.parseObject(JSON.toJSON(result).toString());
		
	}
}
